package com.stone;

import android.app.Activity;
import android.content.Context;
import android.graphics.PathEffect;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.RectF;
/**
 * path路径表示一条曲线
 * 通过路径可以灵活的实现一些效果
 * 该类的几个继承者：ComposePathEffect,CornerPathEffect
 * DashPathEffect,DiscretePathEffect,PathDashPathEffect
 * SumPathEffect完成不同的路径处理。这几个类的作用需要详细处理。
 * */
public class PathActivity extends Activity {
   @Override
protected void onCreate(Bundle savedInstanceState) {
	
	super.onCreate(savedInstanceState);
	setContentView(new MyView(this));
}
   private static class MyView extends View{
    private Paint paint;
    private Path path;
    private PathEffect[] pathEffect;
    private int[] color;
    private float phase;
    //dash猛进，突进，疾奔
    public static PathEffect makeDash(float phase){
    	return new DashPathEffect(new float[]{5,15,8,5}, phase);
    }
    private static void makeEffects(PathEffect[] e,float phase){
    	e[0]=null;//没有效果
    	
    	e[1]=new CornerPathEffect(10);//拐角路径效果
    	e[2]=new DashPathEffect(new float[]{10,5,5,5}, phase);//破折号效果
    	e[3]=new PathDashPathEffect(makePathDash(),12,phase,PathDashPathEffect.Style.ROTATE);//组合路径效果
        //compose组成
    	e[4]=new ComposePathEffect(e[2], e[1]);//组合路径效果（内外各不同）
        e[5]=new ComposePathEffect(e[3], e[1]);//组合效果
        
    }
	public MyView(Context context) {
		super(context);
		setFocusable(true);
		   /**
	     * Set whether this view can receive focus while in touch mode.
	     *ensure担保
	     * Setting this to true will also ensure that this view is focusable.
	     *
	     * @param focusableInTouchMode If true, this view can receive the focus while
	     *   in touch mode.
	     *
	     * @see #setFocusable(boolean)
	     * @attr ref android.R.styleable#View_focusableInTouchMode
	     */
		setFocusableInTouchMode(true);
		paint=new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(6);
		path=makeFollowPath();
		pathEffect=new PathEffect[6];//定义路径效果
		color=new int[]{Color.BLACK,Color.RED,Color.GREEN,Color.MAGENTA,Color.BLUE,Color.BLACK};
		
		
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		RectF bounds=new RectF();
		//compute计算
		path.computeBounds(bounds,false);
		canvas.translate(10-bounds.left, 10-bounds.top);
		makeEffects(pathEffect, phase);//创建几种效果
		phase+=1;
		invalidate();
		
		for(int i=0;i<pathEffect.length;i++){
			paint.setPathEffect(pathEffect[i]);//设置路径效果
			paint.setColor(color[i]);//使用不同颜色
			canvas.drawPath(path,paint);//进行路径绘制
			canvas.translate(0, 28);
			
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch(keyCode){
		case KeyEvent.KEYCODE_DPAD_CENTER://轨迹球确认键
			path=makeFollowPath();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	private static Path makeFollowPath(){
		Path p=new Path();
		p.moveTo(0,0);
		for(int i=1;i<15;i++){
			p.lineTo(i*20,(float)Math.random()*35 );
		}
			return p;
	}
	
	  private static Path makePathDash(){
		  Path p=new Path();
		  /**
		   * contour轮廓，等高线
		     * Set the beginning of the next contour to the point (x,y).
		     *coordinte坐标
		     * @param x The x-coordinate of the start of a new contour
		     * @param y The y-coordinate of the start of a new contour
		     */
		  p.moveTo(4, 0);
		  /**
		     * Add a line from the last point to the specified point (x,y).
		     * If no moveTo() call has been made for this contour, the first point is
		     * automatically set to (0,0).
		     *
		     * @param x The x-coordinate of the end of a line
		     * @param y The y-coordinate of the end of a line
		     */
		  p.lineTo(0, -4);
		  p.lineTo(8, -4);
		  p.lineTo(8, 4);
		  p.lineTo(0,4);
		  return p;
	  } 
   } 
}
